﻿using System;
using System.Collections.Generic;
using System.Linq;
using UnityEngine;

namespace SimpleCore
{
    /// <summary>
    /// 数据管理类
    /// </summary>
    /// <typeparam name="T">数据类类型</typeparam>
    public partial class Data<T> where T : Data<T>
    {
        /// <summary>
        /// 获取指定Id数据
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public static T GetData(int id)
        {
            return _dataMap.TryGetValue(id, out var data) ? data : default;
        }

        /// <summary>
        /// 获取当前数据数量
        /// </summary>
        /// <returns></returns>
        public static int GetDataCount()
        {
            return _dataMap.Count;
        }

        /// <summary>
        /// 获取所有数据
        /// </summary>
        /// <returns></returns>
        public static List<T> GetAllDatas()
        {
            return _dataMap.Values.ToList();
        }

        /// <summary>
        /// 填充数据
        /// </summary>
        /// <param name="datas">数据</param>
        public static void FillDatas(List<object> datas)
        {
            for (int i = 0; i < datas.Count; i++)
            {
                var data = datas[i] as T;
                if (_dataMap.ContainsKey(data.id))
                {
                    Debug.LogError($"Already Contain This Id : {data.id}");
                    continue;
                }

                _dataMap.Add(data.id, data);
            }
        }

        static SortedDictionary<int, T> _dataMap = new SortedDictionary<int, T>();
    }

}